package com.projectsquirrel;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class mainMenu extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		
		
		//setting up the UI -DS
		setContentView(R.layout.main);
		Button submitButton = (Button) findViewById(R.id.fTS_submit);
		final CheckBox ckBox = (CheckBox) findViewById(R.id.checkBox1);
		final EditText email = (EditText) findViewById(R.id.username);
					
			// Setting a button to "listen" for an event -DS
			submitButton.setOnClickListener(new OnClickListener() {
				//on click event -DS
				@Override
				public void onClick(View v) {
					//This is the internal data storage for certain entries such as int,boolean,String,float... -DS
					SharedPreferences pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
					String emailText = "";
					boolean isDoneSetup = false;
					// Display a messagebox.
					Context mContext = getApplicationContext();
					if(!ckBox.isChecked()){
						Toast.makeText(mContext,"Please Accept Terms and Conditions",Toast.LENGTH_LONG).show();						
					}
					else{
						emailText = email.getText().toString();
						String regTest="([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})";
						Pattern p = Pattern.compile(regTest,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
					    Matcher m = p.matcher(emailText);
					    if (!m.find())
					    {
					    	Toast.makeText(mContext,"Please Enter a valid email address",Toast.LENGTH_LONG).show();
					    }
					    else{
					    	isDoneSetup = true;
					    }
					}
					
					if(isDoneSetup==true){
						Toast.makeText(mContext,"Thank you!",Toast.LENGTH_LONG).show();
						fileWrite(pref,"isSetup",true);
						fileWrite(pref,"userEmail",emailText);
						Intent i = new Intent(mainMenu.this, mainMenu1.class);
				        startActivity(i);
				        finish();
					}					
				}
				private void fileWrite(SharedPreferences pref,String key, boolean value) {
					  try {
						  SharedPreferences.Editor editor = pref.edit();  
						  editor.putBoolean(key, value);
						  editor.commit();
					  } 
					  catch (Exception e) {
						  Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
					  }
				}
				private void fileWrite(SharedPreferences pref,String key, String value) {
					  try {
						  SharedPreferences.Editor editor = pref.edit();  
						  editor.putString(key,value);
						  editor.commit();
					  } 
					  catch (Exception e) {
						  Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
					  }
				}
			});

		//}
	}
	/*
	 * This is called after on create before the user can see the screen
	 * http://developer.android.com/reference/android/app/Activity.html
	 * -DS
	 */
	@Override
	public void onStart() {
		//This is the internal data storage for certain entries such as int,boolean,String,float... -DS
		super.onStart();
		//This is the internal data storage for certain entries such as int,boolean,String,float... -DS
		SharedPreferences pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
		boolean setupTest = fileReadBool(pref,"isSetup");
		if(setupTest){
			//Toast.makeText(getApplicationContext(),"App is Set up",Toast.LENGTH_LONG).show();
			Intent i = new Intent(mainMenu.this, mainMenu1.class);
	        startActivity(i);
	        finish();
		}
		else{
			//Toast.makeText(getApplicationContext(),"First Time Setup needed",Toast.LENGTH_LONG).show();
		}
	}
	  private boolean fileReadBool(SharedPreferences pref,String key) {
		  return pref.getBoolean(key, false);
	  }
		 //*/
}